/
Broker`2.cs
334 lines (275 loc) · 12.2 KB
/
Broker`2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright (c) 2020 Sergio Aquilini
// This code is licensed under MIT license (see LICENSE file for details)
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Silverback.Diagnostics;
using Silverback.Messaging.Broker.Behaviors;
using Silverback.Messaging.Configuration;
using Silverback.Util;
namespace Silverback.Messaging.Broker
{
/// <summary>
/// The base class for all <see cref="IBroker" /> implementations.
/// </summary>
/// <typeparam name="TProducerEndpoint">
/// The type of the <see cref="IProducerEndpoint" /> that is being handled by this broker
/// implementation.
/// </typeparam>
/// <typeparam name="TConsumerEndpoint">
/// The type of the <see cref="IConsumerEndpoint" /> that is being handled by this broker
/// implementation.
/// </typeparam>
[SuppressMessage("", "CA1724", Justification = "Preserve backward compatibility since no big deal")]
public abstract class Broker<TProducerEndpoint, TConsumerEndpoint> : IBroker, IDisposable
where TProducerEndpoint : IProducerEndpoint
where TConsumerEndpoint : IConsumerEndpoint
{
private const string CannotCreateConsumerIfConnectedExceptionMessage =
"The broker is already connected. Disconnect it to get a new consumer.";
private const int MaxConnectParallelism = 2;
private const int MaxDisconnectParallelism = 4;
private readonly EndpointsConfiguratorsInvoker _endpointsConfiguratorsInvoker;
private readonly ISilverbackLogger _logger;
private readonly IServiceProvider _serviceProvider;
private readonly List<IConsumer> _consumers = new();
private readonly List<IProducer> _producers = new();
private readonly Dictionary<IEndpoint, IProducer> _endpointToProducerDictionary = new();
/// <summary>
/// Initializes a new instance of the <see cref="Broker{TProducerEndpoint, TConsumerEndpoint}" /> class.
/// </summary>
/// <param name="serviceProvider">
/// The <see cref="IServiceProvider" /> to be used to resolve the required services.
/// </param>
protected Broker(IServiceProvider serviceProvider)
{
_serviceProvider = Check.NotNull(serviceProvider, nameof(serviceProvider));
_endpointsConfiguratorsInvoker =
_serviceProvider.GetRequiredService<EndpointsConfiguratorsInvoker>();
_logger = _serviceProvider
.GetRequiredService<ISilverbackLogger<Broker<IProducerEndpoint, TConsumerEndpoint>>
>();
ProducerEndpointType = typeof(TProducerEndpoint);
ConsumerEndpointType = typeof(TConsumerEndpoint);
}
/// <inheritdoc cref="IBroker.ProducerEndpointType" />
public Type ProducerEndpointType { get; }
/// <inheritdoc cref="IBroker.ConsumerEndpointType" />
public Type ConsumerEndpointType { get; }
/// <inheritdoc cref="IBroker.Producers" />
public IReadOnlyList<IProducer> Producers
{
get
{
if (_producers == null)
throw new ObjectDisposedException(GetType().FullName);
return _producers;
}
}
/// <inheritdoc cref="IBroker.Consumers" />
public IReadOnlyList<IConsumer> Consumers
{
get
{
if (_consumers == null)
throw new ObjectDisposedException(GetType().FullName);
return _consumers;
}
}
/// <inheritdoc cref="IBroker.IsConnected" />
public bool IsConnected { get; private set; }
/// <inheritdoc cref="IBroker.GetProducer(IProducerEndpoint)" />
public virtual IProducer GetProducer(IProducerEndpoint endpoint)
{
Check.NotNull(endpoint, nameof(endpoint));
if (_producers == null)
throw new ObjectDisposedException(GetType().FullName);
var producer = GetOrInstantiateProducer(endpoint);
if (!producer.IsConnected && IsConnected)
AsyncHelper.RunSynchronously(() => producer.ConnectAsync());
return producer;
}
/// <inheritdoc cref="IBroker.GetProducer(string)" />
public virtual IProducer GetProducer(string endpointName)
{
Check.NotEmpty(endpointName, nameof(endpointName));
if (_producers == null)
throw new ObjectDisposedException(GetType().FullName);
return _producers.First(
producer => producer.Endpoint.Name == endpointName ||
producer.Endpoint.FriendlyName == endpointName);
}
/// <inheritdoc cref="IBroker.AddConsumer" />
public virtual IConsumer AddConsumer(IConsumerEndpoint endpoint)
{
Check.NotNull(endpoint, nameof(endpoint));
if (_consumers == null)
throw new ObjectDisposedException(GetType().FullName);
if (IsConnected)
throw new InvalidOperationException(CannotCreateConsumerIfConnectedExceptionMessage);
_logger.LogCreatingNewConsumer(endpoint);
var consumer = InstantiateConsumer(
(TConsumerEndpoint)endpoint,
_serviceProvider.GetRequiredService<IBrokerBehaviorsProvider<IConsumerBehavior>>(),
_serviceProvider);
lock (_consumers)
{
_consumers.Add(consumer);
}
return consumer;
}
/// <inheritdoc cref="IBroker.ConnectAsync" />
public async Task ConnectAsync()
{
if (IsConnected)
return;
if (_producers == null || _consumers == null)
throw new ObjectDisposedException(GetType().FullName);
await _endpointsConfiguratorsInvoker.InvokeAsync().ConfigureAwait(false);
_logger.LogBrokerConnecting(this);
await ConnectAsync(_producers, _consumers).ConfigureAwait(false);
IsConnected = true;
_logger.LogBrokerConnected(this);
}
/// <inheritdoc cref="IBroker.DisconnectAsync" />
public async Task DisconnectAsync()
{
if (!IsConnected)
return;
if (_producers == null || _consumers == null)
throw new ObjectDisposedException(GetType().FullName);
_logger.LogBrokerDisconnecting(this);
await DisconnectAsync(_producers, _consumers).ConfigureAwait(false);
IsConnected = false;
_logger.LogBrokerDisconnected(this);
}
/// <inheritdoc cref="IDisposable.Dispose" />
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Returns a new instance of <see cref="IProducer" /> to publish to the specified endpoint. The
/// returned instance will be cached and reused for the same endpoint.
/// </summary>
/// <param name="endpoint">
/// The endpoint.
/// </param>
/// <param name="behaviorsProvider">
/// The <see cref="IBrokerBehaviorsProvider{TBehavior}" />.
/// </param>
/// <param name="serviceProvider">
/// The <see cref="IServiceProvider" /> instance to be used to resolve the needed types or to be
/// forwarded to the consumer.
/// </param>
/// <returns>
/// The instantiated <see cref="IProducer" />.
/// </returns>
protected abstract IProducer InstantiateProducer(
TProducerEndpoint endpoint,
IBrokerBehaviorsProvider<IProducerBehavior> behaviorsProvider,
IServiceProvider serviceProvider);
/// <summary>
/// Returns a new instance of <see cref="IConsumer" /> to subscribe to the specified endpoint.
/// </summary>
/// <param name="endpoint">
/// The endpoint.
/// </param>
/// <param name="behaviorsProvider">
/// The <see cref="IBrokerBehaviorsProvider{TBehavior}" />.
/// </param>
/// <param name="serviceProvider">
/// The <see cref="IServiceProvider" /> instance to be used to resolve the needed types or to be
/// forwarded to the consumer.
/// </param>
/// <returns>
/// The instantiated <see cref="IConsumer" />.
/// </returns>
protected abstract IConsumer InstantiateConsumer(
TConsumerEndpoint endpoint,
IBrokerBehaviorsProvider<IConsumerBehavior> behaviorsProvider,
IServiceProvider serviceProvider);
/// <summary>
/// Connects all the consumers and starts consuming.
/// </summary>
/// <param name="producers">
/// The producers to be connected.
/// </param>
/// <param name="consumers">
/// The consumers to be connected and started.
/// </param>
/// <returns>
/// A <see cref="Task" /> representing the asynchronous operation.
/// </returns>
protected virtual async Task ConnectAsync(
IReadOnlyCollection<IProducer> producers,
IReadOnlyCollection<IConsumer> consumers)
{
await producers.ParallelForEachAsync(producer => producer.ConnectAsync(), MaxConnectParallelism).ConfigureAwait(false);
await consumers.ParallelForEachAsync(consumer => consumer.ConnectAsync(), MaxConnectParallelism).ConfigureAwait(false);
}
/// <summary>
/// Disconnects all the consumers and stops consuming.
/// </summary>
/// <param name="producers">
/// The producers to be disconnected.
/// </param>
/// <param name="consumers">
/// The consumers to be stopped and disconnected.
/// </param>
/// <returns>
/// A <see cref="Task" /> representing the asynchronous operation.
/// </returns>
protected virtual async Task DisconnectAsync(
IReadOnlyCollection<IProducer> producers,
IReadOnlyCollection<IConsumer> consumers)
{
await producers.ParallelForEachAsync(
producer => producer.DisconnectAsync(),
MaxDisconnectParallelism)
.ConfigureAwait(false);
await consumers.ParallelForEachAsync(
consumer => consumer.DisconnectAsync(),
MaxDisconnectParallelism)
.ConfigureAwait(false);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged
/// resources.
/// </summary>
/// <param name="disposing">
/// A value indicating whether the method has been called by the <c>Dispose</c> method and not from the
/// finalizer.
/// </param>
protected virtual void Dispose(bool disposing)
{
if (!disposing)
return;
AsyncHelper.RunSynchronously(DisconnectAsync);
_consumers.OfType<IDisposable>().ForEach(disposable => disposable.Dispose());
_producers.OfType<IDisposable>().ForEach(disposable => disposable.Dispose());
}
private IProducer GetOrInstantiateProducer(IProducerEndpoint endpoint)
{
if (_endpointToProducerDictionary.TryGetValue(endpoint, out var producer))
return producer;
lock (_producers)
{
if (_endpointToProducerDictionary.TryGetValue(endpoint, out producer))
return producer;
_logger.LogCreatingNewProducer(endpoint);
producer = InstantiateProducer(
(TProducerEndpoint)endpoint,
_serviceProvider.GetRequiredService<IBrokerBehaviorsProvider<IProducerBehavior>>(),
_serviceProvider);
_producers.Add(producer);
_endpointToProducerDictionary.Add(endpoint, producer);
return producer;
}
}
}
}